package com.ccsoft.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * 配合 Ch09NIOServer 测试 NIO 通信
 */
public class Ch09NIOClient {
    public static void main(String[] args) throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);
        InetSocketAddress serverAndPort = new InetSocketAddress("127.0.0.1", 6666);
        if(!socketChannel.connect(serverAndPort)){
            while(!socketChannel.finishConnect()){
                System.out.println("客户端正在连接服务端，但是并不阻塞，此时可以执行其他逻辑~~~");
            }
        }

        String str = "hello NIO~";
        ByteBuffer byteBuffer = ByteBuffer.wrap(str.getBytes());
        socketChannel.write(byteBuffer);
        System.in.read();// 让客户端暂停 - 不要自动关闭
    }
}
